<?php
namespace Admin\Controller;

use Think\Auth;
use Think\Controller;

class CommonController extends Controller
{
    // 不需要登录验证的url，不能使用大写，不然有bug
    protected $without = [
        '/login/index',
        '/login/login',
        '/login/logout',
    ];
    /**
     * 构建函数
     */
    public function __construct()
    {
        parent::__construct();

        // 是否需要登录检查
        if ($this->_loginHandle()) {
            // 登录验证
            $user = $this->_checkLogin();

            // 权限处理
            $this->_handleAuth($user);

            // 菜单处理
            $this->_handleMenu($user);
        }

        // 缓存控制
        $this->_cache();
    }

    /**
     * 检查是否过期
     * @return [type] [description]
     */
    private function _checkLogin()
    {
        $admin = session('admin_user');
        if ($admin) {
            return $admin;
        }
        $this->redirect(U('login/index'));
    }

    /**
     * 权限管理
     * @param  [type] $user [description]
     * @return [type]       [description]
     */
    private function _handleAuth($user)
    {
        $auth      = new Auth();
        $rule_name = '/' . CONTROLLER_NAME . '/' . ACTION_NAME;

        if (!$auth->check($rule_name, $user['id']) && !in_array($user['id'], C('AUTH_CONFIG')['AUTH_SUPER'])) {
            $this->error('您没有访问的权限');
        }

        if (!$user['groups']) {
            $groups = $auth->getGroups($user['id']);
            foreach ($groups as $group) {
                $groups_name .= '/' . $group['title'];
            }

            $groups_name = substr($groups_name, 1);
            session('admin_user.groups', $groups_name);
        }
    }

    /**
     * 菜单处理
     * @param  [type] $user [description]
     * @return [type]       [description]
     */
    private function _handleMenu($user)
    {
        $auth = new Auth();
        if (!session('?menu')) {
            $menu = get_conf('menu');
            // 处理
            if (!in_array($user['id'], C('AUTH_CONFIG')['AUTH_SUPER'])) {

                $authList = $auth->getAuthList($user['id'], 1);

                foreach ($menu as $key => $value) {
                    if ($value['url'] && !in_array(strtolower($value['url']), $authList)) {
                        unset($menu[$key]);
                    }
                    if ($value['sub']) {
                        $delete = 0;
                        foreach ($value['sub'] as $k => $v) {
                            if (!in_array(strtolower($v['url']), $authList)) {
                                unset($menu[$key]['sub'][$k]);
                                $delete++;
                            }
                        }
                        if ($delete == $k + 1) {
                            unset($menu[$key]);
                        }
                    }
                }
            }
            session('menu', $menu);
        }
        $this->assign('menu', session('menu'));
    }

    /**
     * 登录控制，处理不需要登录的控制器
     * @return [type] [description]
     */
    private function _loginHandle()
    {
        return $is_check = !in_array(strtolower("/" . CONTROLLER_NAME . "/" . ACTION_NAME), $this->without);
    }

    private function _cache()
    {
        // 设置缓存
        S([
            'type'   => 'File',
            'prefix' => 'admin_',
            'expire' => 7 * 24 * 3600,
        ]);
    }

    /**
     * 删除 建议子类重写
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('post.id');

        $is = $this->model->delete($id);

        if ($is) {
            success([
                'id' => $id,
            ]);
        } else {
            error(1008, '删除失败');
        }
    }
}
